package com.kc.bus.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kc.bus.domain.Customer;
import com.kc.bus.service.CustomerService;
import com.kc.bus.vo.CustomerVo;
import com.kc.sys.common.DataGridView;
import com.kc.sys.common.R;
import com.kc.sys.domain.User;
import com.kc.sys.util.WebUtils;
import com.kc.sys.vo.NoticeVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老杜
 * @since 2019-10-28
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @RequestMapping("loadAllCustomer")
    public DataGridView loadAllCustomer(CustomerVo customerVo){

        IPage<Customer> page = new Page<>(customerVo.getPage(),customerVo.getLimit());
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(customerVo.getCustomername()),"customername",customerVo.getCustomername());
        queryWrapper.like(StringUtils.isNotBlank(customerVo.getAddress()),"address",customerVo.getAddress());
        queryWrapper.like(StringUtils.isNotBlank(customerVo.getConnectionperson()),"connectionperson",customerVo.getConnectionperson());
        customerService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    //添加客户
    @RequestMapping("addCustomer")
    public R addCustomer(CustomerVo customerVo){
        try {
            User user = (User) WebUtils.getSession().getAttribute("user");
            customerService.save(customerVo);
            return R.ok().message("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("添加失败");
        }
    }
    //更新客户
    @RequestMapping("updateCustomer")
    public R updateCustomer(CustomerVo customerVo){
        try {
            customerService.updateById(customerVo);
            return R.ok().message("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("更新失败");
        }
    }
    /**
     * 根据Id 删除
     * */
    @RequestMapping("deleteCustomer")
    public R deleteById(Integer id){
        try {
            customerService.removeById(id);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }

    /**
     * 批量删除
     * @param --logInfoVo
     * @return
     */
    @RequestMapping("deleteCustomers")
    public R deleteByIds(CustomerVo customerVo){
        try {
            ArrayList<Serializable> ids = new ArrayList<>();
            for (Integer id : customerVo.getIds()) {
                ids.add(id);
            }
            customerService.removeByIds(ids);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }

    @RequestMapping("uploadFile")
    public DataGridView uploadFile(HttpServletRequest request, @Param("file") MultipartFile file){

        return new DataGridView();
    }
}

